/**
 * Created by WT-SUN on 2017/4/14.
 */
$(function () {
    $('#btn_save').on("click", function () {
        submitForm();
    });
    queryDepartmentTree();
    //hazardFactorData();
    /**
     * 绑定监测单位选择事件
     */
    $("#serverIdName").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                openSerViceTb();
            }
        },{
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');
                $("#serverId").val('');
                $("#aptitudeNumber").textbox('setValue', '').textbox('setText', '');
            }
        }]
    });
    var isMonitorData=[{id:'0',text:'是'},{id:'1',text:'否',selected:true}];
    $("#isOverproof").combobox({
        data: isMonitorData,
        valueField: 'id',
        textField: 'text',
        editable: false,
        panelHeight: '150',
        selectOnNavigation:true,
        onSelect:function (r) {//cxNum
            if(r.id=='1'){
                $('#cxNum').textbox('setValue','').textbox({disabled:true});
                $('#cbNum').textbox('setValue','').textbox({disabled:true});
            }else{
                $('#cxNum').textbox('setValue','').textbox({disabled:false});
                $('#cbNum').textbox('setValue','').textbox({disabled:false});
            }
        }
    });

    
    
    
    
    $("#isMonitor").combobox({
        data: isMonitorData,
        valueField: 'id',
        textField: 'text',
        editable: false,
        panelHeight: '150',
        selectOnNavigation:true,
        onSelect:function (record) {
            if (record.id=='0'){
                $("#serverIdName").textbox('setValue', "").textbox('setText', "");
                $("#serverId").val("");
                $("#aptitudeNumber").textbox('setValue', "").textbox('setText', "");
                $("#serverIdName").textbox({
                    required:false,
                    readonly:true
                });
                $("#reSerId").hide();
            }else {
                $("#serverIdName").textbox({
                    required:true,
                    readonly:false
                });
                $("#reSerId").show();
            }
        }
    });
    $('#positionId').combobox({
        valueField: 'id',
        textField: 'text',
        prompt:'请选择'
    });
    $("#monitorProjectName").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                add_hazardFactor_func();
            }
        },{
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');
                $('#monitorProjectId').val('');
            }
        }]
    });
    queryYearthList("yearAnnual");


});
var add_hazardFactor_func=function () {
    WindowManager.openLayer({
        type: 2,
        title: '选择危害因素',
        area: ['900px', '60%'],
        content: ROOT_PATH + '/occupationalHealth/hazardFactor/listTb.shtml?isMulti=0',
        callBack:function(data){
            getSelectedFactor(data);
        }
    });
};
function queryYearthList(id){
    var date = new Date();
    var year = date.getFullYear();
    var yearthData = new Array();
    for(var i =0;i<6;i++){
        var y = {id:year-i,text:(year-i)+'年'};
        yearthData.push(y);
    }
    $('#'+id).combobox(
        {
            data:yearthData,
            valueField:'id',
            textField:'text',
            required:true,
            onLoadSuccess: function () { //加载完成后,设置选中第一项
                $(this).combobox('select', yearthData[0].id);
            }
        }
    );
}
var getSelectedFactor = function (data) {
    $("#monitorProjectName").textbox('setValue', data[0].id).textbox('setText', data[0].name);
    $("#monitorProjectId").val(data[0].id);
};
function submitForm() {
    if($('#companyNameId').val()==''){
        layer.alert('请选择单位名称');
        return;
    }
    if($('#monitorProjectName').val()==''){
        layer.alert('请填写监测项目名称');
        return;
    }
    if($('#monitorProjectId').val()==''){
        layer.alert('监测项目不正确');
        return;
    }

    if($('#isMonitor').combobox("getValue")=='1'){
        if($('#serverIdName').val()==''){
            layer.alert('请填写监测单位名称');
            return;
        }
        if($('#serverId').val()==''){
            layer.alert('监测单位不正确');
            return;
        }
    }

    $('#formStation').form('submit', {
        url: ROOT_PATH + '/occupationalHealth/station/insertSelective.shtml',
        onSubmit: function (param) {
            if (!$(this).form('validate')) {
                layer.alert('请正确填写信息', {icon: 5});
            }
            return $(this).form('validate');
        },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.success) {
                layer.msg('保存成功', {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function () {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                    $.getIframeWindow().reLoadData();
                });
            }
            else {
                layer.msg('保存失败', {
                    icon: 2,
                    time: 1000
                });
            }
        }
    });
}

function openSerViceTb() {
    WindowManager.openLayer({
        type: 2,
        title: '选择服务机构',
        area: ['850px', '70%'],
        content: ROOT_PATH + '/occupationalHealth/hseServiceOrg/orgTb.shtml',
        callBack:function(data){
            getSelectedOrg(data);
        }
    });
}
function getSelectedOrg(data) {
    $("#serverIdName").textbox('setValue', data[0].serviceName).textbox('setText', data[0].serviceName);
    $("#serverId").val(data[0].id);
    $("#aptitudeNumber").textbox('setValue', data[0].aptitudeNumber).textbox('setText', data[0].aptitudeNumber);
}

/**
 * 查询部门树方法
 */
function queryDepartmentTree(){
    $.ajax({
        url:ROOT_PATH + '/permission/department/getDepartmentTree.shtml',//提交地址
        data:{parentId : 0},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){
            $('#companyNameId').combotree(
                {
                    data:result,
                    require:true,
                    loadFilter:function(data){
                        var newData = new Array();
                        newData.push({id:"",text:'请选择'});
                        newData.push(data[0]);
                        return newData;
                    },
                    onSelect:function (record){
                        $.ajax({
                            url:ROOT_PATH + '/permission/post/findByDeptId.shtml?deptId='+record.id,//提交地址
                            type:"POST",
                            dataType:"json",
                            success:function(result){
                                $('#positionId').combobox({
                                    prompt:'请选择',
                                    data:result,
                                    require:true
                                });
                            }
                        });
                    }
                }
            );
        }
    });
}